from bs4 import BeautifulSoup
import urllib.request
import sqlite3

class weatherdb:
    def __init__(self):
        self.con = sqlite3.connect('weathers.db')
        self.cursor = self.con.cursor()
        try:
            self.cursor.execute("create table weathers(city varchar(32), date varchar(16), weather varchar(64), temp varchar(32), constraint pk_weathers primary key(city,date))")
            self.cursor.execute("delete from weathers")
        except:
            pass

    def close(self):
        self.con.commit()
        self.con.close()

    def insert(self, city, date, weather, temp):
        self.cursor.execute("insert into weathers(city,date,weather,temp) values(?,?,?,?)", (city, date, weather, temp))


class WeatherForecast:
    def __init__(self):
        self.headers = {'User - Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'}
        self.cityCode = {"北京": "101010100", "上海": "101020100", "广州": "101280101", "深圳": "101280601"}

    def forecastCity(self, city):
        if city not in self.cityCode.keys():
            print(city + " code cannot be found")
            return
        url = "https://www.weather.com.cn/weather/" + self.cityCode[city] + ".shtml"
        print(f"正在爬取 {city} 的天气数据: {url}")
        try:
            req = urllib.request.Request(url, headers=self.headers)
            data = urllib.request.urlopen(req)
            print(f"成功获取 {city} 网页内容，状态码: {data.status}")
            data = data.read()
            dammit = BeautifulSoup(data, "lxml")
            lis = dammit.select("ul.t.clearfix li")
            print(f"找到 {len(lis)} 个天气条目")
            for li in lis:
                date_elems = li.select('h1')
                date = date_elems[0].text if date_elems else '未知'
                weather_elems = li.select('p.wea')
                weather = weather_elems[0].text.strip() if weather_elems else '未知'
                temp_spans = li.select('p.tem span')
                temp_ils = li.select('p.tem i')
                high_temp = temp_spans[0].text.strip() if temp_spans else '未知'
                low_temp = temp_ils[0].text.strip() if temp_ils else '未知'
                temp = f"{high_temp}/{low_temp}"
                self.db.insert(city, date, weather, temp)
        except Exception as err:
            print(f"爬取 {city} 天气数据时发生错误: {err}")

    def process(self, cities):
        self.db = weatherdb()
        for city in cities:
            self.forecastCity(city)
        self.db.close()


if __name__ == '__main__':
    wf = WeatherForecast()
    cities = ["北京", "上海", "广州", "深圳"]
    wf.process(cities)
